/*
 * 著作权声明
 * Copyright (c) 2025.8 拾壹
 * 本代码为个人独立开发，未经本人书面许可，任何组织或个人不得擅自复制、修改、分发、用于商业用途。
 * 联系方式：sjw123521@qq.com
 */
package com.xmqsfsky1017.bzgzs;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class BaauzougungNumber extends BigDecimal {
    private static final String[] CNNUMBERS = {"X", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] CNUNITS = {"佰", "拾", "万", "仟", "佰", "拾", "元"};
    private static final String[] DECIMALUNITS = {"角", "分"};

    public BaauzougungNumber(String x) {
        super(x);
    }

    /**
     * 转换为中文大写金额字符串
     * @return 中文大写金额字符串
     */
    public String toBigWrite() {
		
		BigDecimal integerPart = new BigDecimal(this.toString()).setScale(2, RoundingMode.DOWN);
        if (integerPart.precision() > 6) {
            return "（数值整数部分过长，最多支持6位整数）";
        }
	
        if (this.compareTo(BigDecimal.ZERO) == 0) {
            return "X佰X拾X万X仟X佰X拾X元X角X分";
        }

        StringBuilder result = new StringBuilder();
        boolean isNegative = this.compareTo(BigDecimal.ZERO) < 0;
        BigDecimal absValue = isNegative ? this.abs() : this;

        long yuan = absValue.setScale(2, BigDecimal.ROUND_DOWN).longValue();
        int decimalPart = (int) ((absValue.subtract(new BigDecimal(yuan))).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_DOWN).longValue());
        int jiao = decimalPart / 10;
        int fen = decimalPart % 10;

        if (isNegative) {
            result.append("负");
        }

        long[] digits = new long[6];
        digits[0] = yuan / 1000000;
        digits[1] = (yuan % 1000000) / 100000;
        digits[2] = (yuan % 100000) / 10000;
        digits[3] = (yuan % 10000) / 1000;
        digits[4] = (yuan % 1000) / 100;
        digits[5] = (yuan % 100) / 10;
        long geWei = yuan % 10;

        for (int i = 0; i < digits.length; i++) {
            long digit = digits[i] % 10;
            result.append(CNNUMBERS[(int) digit]).append(CNUNITS[i]);
        }

        result.append(CNNUMBERS[(int) (geWei % 10)]).append(CNUNITS[6]);
        result.append(CNNUMBERS[jiao]).append(DECIMALUNITS[0]);
        result.append(CNNUMBERS[fen]).append(DECIMALUNITS[1]);

        return result.toString();
    }
    
    /** 以下为新增的比较方法 */
    
    /**
     * 比较当前数值是否大于另一个值
     * @param val 比较对象
     * @return true 如果当前值大于val
     */
    public boolean gt(BaauzougungNumber val) {
        return this.compareTo(val) > 0;
    }

    /**
     * 比较当前数值是否小于另一个值
     * @param val 比较对象
     * @return true 如果当前值小于val
     */
    public boolean lt(BaauzougungNumber val) {
        return this.compareTo(val) < 0;
    }

    /**
     * 比较当前数值是否大于或等于另一个值
     * @param val 比较对象
     * @return true 如果当前值大于或等于val
     */
    public boolean ge(BaauzougungNumber val) {
        return this.compareTo(val) >= 0;
    }

    /**
     * 比较当前数值是否小于或等于另一个值
     * @param val 比较对象
     * @return true 如果当前值小于或等于val
     */
    public boolean le(BaauzougungNumber val) {
        return this.compareTo(val) <= 0;
    }

    /**
     * 检查当前数值是否等于零
     * @return true 如果当前值为零
     */
    public boolean isZero() {
        return this.compareTo(BigDecimal.ZERO) == 0;
    }

    /**
     * 检查当前数值是否为负数
     * @return true 如果当前值小于零
     */
    public boolean isNegative() {
        return this.compareTo(BigDecimal.ZERO) < 0;
    }

    /**
     * 检查当前数值是否为正数
     * @return true 如果当前值大于零
     */
    public boolean isPositive() {
        return this.compareTo(BigDecimal.ZERO) > 0;
    }

    /** 以下为算术运算方法重写 - 返回本类实例 */
    
    public BaauzougungNumber add(BaauzougungNumber val) {
        BigDecimal result = super.add(val);
        return new BaauzougungNumber(result.toString());
    }

    public BaauzougungNumber subtract(BaauzougungNumber val) {
        BigDecimal result = super.subtract(val);
        return new BaauzougungNumber(result.toString());
    }

    public BaauzougungNumber multiply(BaauzougungNumber val) {
        BigDecimal result = super.multiply(val);
        return new BaauzougungNumber(result.toString());
    }

    public BaauzougungNumber divide(BaauzougungNumber val) {
        BigDecimal result = super.divide(val, 2, RoundingMode.HALF_UP);
        return new BaauzougungNumber(result.toString());
    }

    public BaauzougungNumber multiply(BigDecimal val) {
        BigDecimal result = super.multiply(val);
        return new BaauzougungNumber(result.toString());
    }

    public BaauzougungNumber divide(BigDecimal val) {
        BigDecimal result = super.divide(val, 2, RoundingMode.HALF_UP);
        return new BaauzougungNumber(result.toString());
    }

    public BaauzougungNumber abs() {
        BigDecimal result = super.abs();
        return new BaauzougungNumber(result.toString());
    }

    public BaauzougungNumber negate() {
        BigDecimal result = super.negate();
        return new BaauzougungNumber(result.toString());
    }

    public BaauzougungNumber pow(int n) {
        BigDecimal result = super.pow(n, new MathContext(10));
        return new BaauzougungNumber(result.toString());
    }
}
